package com.pinyoumei.app.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pinyoumei.app.R;
import com.liang.jtablayout.tab.Tab;
import com.liang.widget.JTabLayout;

import java.util.ArrayList;

/**
 * 地区选择弹窗
 */
public class AreaSelectDialog extends Dialog implements View.OnClickListener{
    private Context context;
    private int layoutResID;
    private int[] listenedItems;
    private JTabLayout tabLayout;
    private RelativeLayout relative_cancle;
    private RecyclerView recyclerview1,recyclerview2,recyclerview3;
    private MyRecyclerViewAdapter myRecyclerViewAdapter;
    private MyRecyclerViewAdapter2 myRecyclerViewAdapter2;
    private MyRecyclerViewAdapter3 myRecyclerViewAdapter3;
    private ArrayList<String> options1Items = new ArrayList<>();//一级菜单数据
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();//二级菜单数据
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();//三级菜单数据
    private int position1,position2,position3=0;
    private boolean flags1 = false;
    private boolean flags2 = false;
    private boolean flags3 = false;
    private String string1,string2,string3;
    public AreaSelectDialog(Context context){
        super(context);
        this.context = context;
    }
    public AreaSelectDialog(Context context, int themeResId){
        super(context, themeResId);
        this.context = context;
    }
    public AreaSelectDialog(Context context, boolean cancelable, OnCancelListener cancelListener){
        super(context, cancelable, cancelListener);
    }
    public AreaSelectDialog(Context context, int layoutResID, int[] listenedItems, ArrayList<String> options1Items, ArrayList<ArrayList<String>> options2Items, ArrayList<ArrayList<ArrayList<String>>> options3Items){
        super(context, R.style.dialogStyle);//这个决定dialog的样式
        this.context = context;
        this.layoutResID = layoutResID;
        this.listenedItems = listenedItems;
        this.options1Items = options1Items;
        this.options2Items = options2Items;
        this.options3Items = options3Items;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();//获取一个窗口
        window.setGravity(Gravity.BOTTOM);//设置窗口的位置
        window.setWindowAnimations(R.style.bottom_menu_animation);//设置窗口进入时的动画特效
        WindowManager.LayoutParams layoutParams = window.getAttributes();//获取窗口的属性赋值给layoutParams
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);//设置窗口的属性
        setCanceledOnTouchOutside(true);
        setContentView(R.layout.dialog_areaselect_layout);
        tabLayout = findViewById(R.id.tabLayout);

        Tab tabItem1 = tabLayout.newTab();//创建一个TabView
        tabItem1.setTitle("请选择");
        tabLayout.addTab(tabItem1);

        relative_cancle = findViewById(R.id.relative_cancle);
        relative_cancle.setOnClickListener(this);
        //省份的数据加载
        recyclerview1 = findViewById(R.id.recyclerview1);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        recyclerview1.setLayoutManager(linearLayoutManager);
        myRecyclerViewAdapter = new MyRecyclerViewAdapter(context,options1Items);
        recyclerview1.setAdapter(myRecyclerViewAdapter);

        recyclerview2 = findViewById(R.id.recyclerview2);
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(context);
        recyclerview2.setLayoutManager(linearLayoutManager2);

        recyclerview3 = findViewById(R.id.recyclerview3);
        LinearLayoutManager linearLayoutManager3 = new LinearLayoutManager(context);
        recyclerview3.setLayoutManager(linearLayoutManager3);

        recyclerview1.setVisibility(View.VISIBLE);
        recyclerview2.setVisibility(View.GONE);
        recyclerview3.setVisibility(View.GONE);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.relative_cancle:
                dismiss();
                break;
        }
    }
    @Override
    public void show(){
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
    }
    //自定义一个监听的接口回调
    public DialogClickListener listener;
    public interface DialogClickListener{
        void onClick(String string);
    }
    public void setOnClickListener(DialogClickListener listener){
        this.listener = listener;
    }
    //省份的适配器
    class MyRecyclerViewAdapter extends RecyclerView.Adapter{
        private Context context;
        private ArrayList<String> list;
        public MyRecyclerViewAdapter(Context context,ArrayList<String> list){
            this.context = context;
            this.list = list;
        }
        @Override
        public long getItemId(int position){
            return position;
        }
        @Override
        public int getItemCount(){
            return list.size();
        }
        @Override
        public int getItemViewType(int position){
            return position;
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position){
            View view = LayoutInflater.from(context).inflate(R.layout.item_layout_areselectdialog,viewGroup,false);
            return new MyViewHolder(view);
        }
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position){
            if(position == position1 && flags1==true){//显示沟
                ((MyViewHolder)viewHolder).image_select.setVisibility(View.VISIBLE);
            }else{
                ((MyViewHolder)viewHolder).image_select.setVisibility(View.GONE);
            }
            ((MyViewHolder)viewHolder).text_area.setText(list.get(position));//设置身份
            ((MyViewHolder)viewHolder).linear_click.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    flags1 = true;
                    position1 = position;//记录第一个列表点击的下标
                    tabLayout.removeAllTabs();

                    //点击省份将身份加入tablelayout控件
                    Tab tab_province = tabLayout.newTab();//创建一个TabView
                    tab_province.setTitle(list.get(position));
                    string1 = list.get(position);//省份
                    tabLayout.addTab(tab_province);

                    Tab tab_select = tabLayout.newTab();//创建一个TabView
                    tab_select.setTitle("请选择");
                    tabLayout.addTab(tab_select);
                    //市区的数据加载
                    myRecyclerViewAdapter2 = new MyRecyclerViewAdapter2(context,options2Items.get(position1));
                    recyclerview2.setAdapter(myRecyclerViewAdapter2);

                    recyclerview1.setVisibility(View.GONE);
                    recyclerview2.setVisibility(View.VISIBLE);
                    recyclerview3.setVisibility(View.GONE);
                    tabLayout.selectTab(1);//定位到请选择
                    tabLayout.addOnTabSelectedListener(new JTabLayout.OnTabSelectedListener() {
                        @Override
                        public void onTabSelected(@NonNull Tab var1){
                            switch (var1.getPosition()){
                                case 0://点击了省份的item
                                    myRecyclerViewAdapter = new MyRecyclerViewAdapter(context,options1Items);
                                    recyclerview1.setAdapter(myRecyclerViewAdapter);
                                    recyclerview1.setVisibility(View.VISIBLE);
                                    recyclerview2.setVisibility(View.GONE);
                                    recyclerview3.setVisibility(View.GONE);
                                    break;
                                case 1://点击了请选择
                                    //市区的数据加载
                                    myRecyclerViewAdapter2 = new MyRecyclerViewAdapter2(context,options2Items.get(position1));
                                    recyclerview2.setAdapter(myRecyclerViewAdapter2);
                                    recyclerview1.setVisibility(View.GONE);
                                    recyclerview2.setVisibility(View.VISIBLE);
                                    recyclerview3.setVisibility(View.GONE);
                                    break;
                            }

                        }
                        @Override
                        public void onTabUnselected(@NonNull Tab var1){

                        }
                        @Override
                        public void onTabReselected(@NonNull Tab var1){

                        }
                    });
                }
            });
        }
        class MyViewHolder extends RecyclerView.ViewHolder{
            private ImageView image_select;
            private TextView text_area;
            private LinearLayout linear_click;
            public MyViewHolder(View view){
                super(view);
                image_select = view.findViewById(R.id.image_select);
                text_area = view.findViewById(R.id.text_area);
                linear_click = view.findViewById(R.id.linear_click);
            }
        }
    }

    //市的适配器
    class MyRecyclerViewAdapter2 extends RecyclerView.Adapter{
        private Context context;
        private ArrayList<String> list;
        public MyRecyclerViewAdapter2(Context context,ArrayList<String> list){
            this.context = context;
            this.list = list;
        }
        @Override
        public long getItemId(int position){
            return position;
        }
        @Override
        public int getItemCount(){
            return list.size();
        }
        @Override
        public int getItemViewType(int position){
            return position;
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position){
            View view = LayoutInflater.from(context).inflate(R.layout.item_layout_areselectdialog,viewGroup,false);
            return new MyViewHolder(view);
        }
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position){
            if(position == position2 && flags2 ==true){//显示沟
                ((MyViewHolder)viewHolder).image_select.setVisibility(View.VISIBLE);
            }else{
                ((MyViewHolder)viewHolder).image_select.setVisibility(View.GONE);
            }
            ((MyViewHolder)viewHolder).text_area.setText(list.get(position));//设置身份
            ((MyViewHolder)viewHolder).linear_click.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    flags2 = true;
                    position2 = position;//记录第二个列表点击的下标
                    tabLayout.removeAllTabs();
                    //省份的tab
                    Tab tab_province = tabLayout.newTab();//创建一个TabView
                    tab_province.setTitle(options1Items.get(position1));
                    tabLayout.addTab(tab_province);

                    //将市区数据加入tablelayout控件
                    Tab tab_qu = tabLayout.newTab();//创建一个TabView
                    tab_qu.setTitle(list.get(position2));
                    string2 = list.get(position2);//市
                    tabLayout.addTab(tab_qu);

                    Tab tab_select = tabLayout.newTab();//创建一个TabView
                    tab_select.setTitle("请选择");
                    tabLayout.addTab(tab_select);

                    //区的数据加载
                    myRecyclerViewAdapter3 = new MyRecyclerViewAdapter3(context,options3Items.get(position1).get(position2));
                    recyclerview3.setAdapter(myRecyclerViewAdapter3);

                    recyclerview1.setVisibility(View.GONE);
                    recyclerview2.setVisibility(View.GONE);
                    recyclerview3.setVisibility(View.VISIBLE);
                    tabLayout.selectTab(tabLayout.getTabCount()-1);
                    tabLayout.addOnTabSelectedListener(new JTabLayout.OnTabSelectedListener() {
                        @Override
                        public void onTabSelected(@NonNull Tab var1) {

                            switch (var1.getPosition()){
                                case 0://省份
                                    myRecyclerViewAdapter = new MyRecyclerViewAdapter(context,options1Items);
                                    recyclerview1.setAdapter(myRecyclerViewAdapter);
                                    recyclerview1.setVisibility(View.VISIBLE);
                                    recyclerview2.setVisibility(View.GONE);
                                    recyclerview3.setVisibility(View.GONE);
                                    break;
                                case 1://市
                                    //市区的数据加载
                                    myRecyclerViewAdapter2 = new MyRecyclerViewAdapter2(context,options2Items.get(position1));
                                    recyclerview2.setAdapter(myRecyclerViewAdapter2);
                                    recyclerview1.setVisibility(View.GONE);
                                    recyclerview2.setVisibility(View.VISIBLE);
                                    recyclerview3.setVisibility(View.GONE);
                                    break;
                                case 2://请选择
                                    //区的数据加载
                                    myRecyclerViewAdapter3 = new MyRecyclerViewAdapter3(context,options3Items.get(position1).get(position2));
                                    recyclerview3.setAdapter(myRecyclerViewAdapter3);
                                    recyclerview1.setVisibility(View.GONE);
                                    recyclerview2.setVisibility(View.GONE);
                                    recyclerview3.setVisibility(View.VISIBLE);
                                    break;
                            }
                        }

                        @Override
                        public void onTabUnselected(@NonNull Tab var1) {

                        }

                        @Override
                        public void onTabReselected(@NonNull Tab var1) {

                        }
                    });
                }
            });
        }
        class MyViewHolder extends RecyclerView.ViewHolder{
            private ImageView image_select;
            private TextView text_area;
            private LinearLayout linear_click;
            public MyViewHolder(View view){
                super(view);
                image_select = view.findViewById(R.id.image_select);
                text_area = view.findViewById(R.id.text_area);
                linear_click = view.findViewById(R.id.linear_click);
            }
        }
    }

    //区的适配器
    class MyRecyclerViewAdapter3 extends RecyclerView.Adapter{
        private Context context;
        private ArrayList<String> list;
        public MyRecyclerViewAdapter3(Context context,ArrayList<String> list){
            this.context = context;
            this.list = list;
        }
        @Override
        public long getItemId(int position){
            return position;
        }
        @Override
        public int getItemCount(){
            return list.size();
        }
        @Override
        public int getItemViewType(int position){
            return position;
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position){
            View view = LayoutInflater.from(context).inflate(R.layout.item_layout_areselectdialog,viewGroup,false);
            return new MyViewHolder(view);
        }
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position){
            if(position == position3 && flags3 ==true){//显示沟
                ((MyViewHolder)viewHolder).image_select.setVisibility(View.VISIBLE);
            }else{
                ((MyViewHolder)viewHolder).image_select.setVisibility(View.GONE);
            }
            ((MyViewHolder)viewHolder).text_area.setText(list.get(position));//设置身份
            ((MyViewHolder)viewHolder).linear_click.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    flags3 = true;
                    position3 = position;//记录第三个列表点击的下标
                    tabLayout.removeAllTabs();
                    //省份的tab
                    Tab tab_province = tabLayout.newTab();//创建一个TabView
                    tab_province.setTitle(options1Items.get(position1));
                    tabLayout.addTab(tab_province);

                    //市数据加入
                    Tab tab_city = tabLayout.newTab();//创建一个TabView
                    tab_city.setTitle(options2Items.get(position1).get(position2));
                    tabLayout.addTab(tab_city);

                    //区的数据加入
                    Tab tab_qu = tabLayout.newTab();
                    tab_qu.setTitle(options3Items.get(position1).get(position2).get(position));
                    string3 = options3Items.get(position1).get(position2).get(position);//区
                    tabLayout.addTab(tab_qu);

                    tabLayout.selectTab(tabLayout.getTabCount()-1);
                    tabLayout.addOnTabSelectedListener(new JTabLayout.OnTabSelectedListener() {
                        @Override
                        public void onTabSelected(@NonNull Tab var1) {
                            switch (var1.getPosition()){
                                case 0://省份
                                    myRecyclerViewAdapter = new MyRecyclerViewAdapter(context,options1Items);
                                    recyclerview1.setAdapter(myRecyclerViewAdapter);
                                    recyclerview1.setVisibility(View.VISIBLE);
                                    recyclerview2.setVisibility(View.GONE);
                                    recyclerview3.setVisibility(View.GONE);
                                    break;
                                case 1://市
                                    //市区的数据加载
                                    myRecyclerViewAdapter2 = new MyRecyclerViewAdapter2(context,options2Items.get(position1));
                                    recyclerview2.setAdapter(myRecyclerViewAdapter2);
                                    recyclerview1.setVisibility(View.GONE);
                                    recyclerview2.setVisibility(View.VISIBLE);
                                    recyclerview3.setVisibility(View.GONE);
                                    break;
                                case 2://区
                                    //区的数据加载
                                    myRecyclerViewAdapter3 = new MyRecyclerViewAdapter3(context,options3Items.get(position1).get(position2));
                                    recyclerview3.setAdapter(myRecyclerViewAdapter3);
                                    recyclerview1.setVisibility(View.GONE);
                                    recyclerview2.setVisibility(View.GONE);
                                    recyclerview3.setVisibility(View.VISIBLE);
                                    break;
                            }
                        }

                        @Override
                        public void onTabUnselected(@NonNull Tab var1) {

                        }

                        @Override
                        public void onTabReselected(@NonNull Tab var1) {

                        }
                    });

                    //将Tab上的所有信息拼接 成省市区 然后将结果回调出去 弹窗消失
                    dismiss();
                    String string = (string1+","+string2+","+string3).trim();//剔除空格 以"|"分割
                    listener.onClick(string);
                }
            });
        }
        class MyViewHolder extends RecyclerView.ViewHolder{
            private ImageView image_select;
            private TextView text_area;
            private LinearLayout linear_click;
            public MyViewHolder(View view){
                super(view);
                image_select = view.findViewById(R.id.image_select);
                text_area = view.findViewById(R.id.text_area);
                linear_click = view.findViewById(R.id.linear_click);
            }
        }
    }
}
